﻿using System.Drawing;
using System.Windows.Forms;

namespace SharpCmd.Windows.Forms
{
    /// <summary>
    /// if wa have a long Text like
    /// d:\Program Files\Microsoft Visual Studio 10.0\Microsoft Visual C# 2010 Express Beta 2 - ENU\Logs 
    /// it's clipped to our Width like this:
    /// ...0.0\Microsoft Visual C# 2010 Express Beta 2 - ENU\Logs
    /// with ellipsis on the left side 
    /// </summary>
    internal class LeftEllipsisLabel : Label
    {
        private const string Ellipsis = "...";
        private float EllipsisWidth;
        protected override void OnPaint(PaintEventArgs e)
        {
            if (EllipsisWidth == 0)
                EllipsisWidth = e.Graphics.MeasureString(Ellipsis, Font).Width;
            float stringWidth = e.Graphics.MeasureString(Text, Font).Width;
            if (stringWidth > Width)
            {
                float wantedWidth = this.Width - EllipsisWidth;
                //need to cut off beginning of the string and replace with ellipsis
                //TODO!! fix naive implementation, implement some recursive partitioning clipping
                for (int i = 0; i < Text.Length; i++)
                {
                    string tempString = Text.Substring(i);
                    if (e.Graphics.MeasureString(tempString, Font).Width < this.Width)
                    {
                        e.Graphics.DrawString(Ellipsis + tempString, Font, Brushes.Black, 0, 0);
                        return;
                    }
                }
            }
            e.Graphics.DrawString(Text, Font, Brushes.Black, 0, 0);
        }
    }
}
